﻿@using OA.Model; 
@model OA.Model.Voucher
@{
    Layout = "~/Areas/Member/Views/Shared/_Layer_Layout.cshtml";
    List<SelectListItem> list = ViewBag.SelectListItem;
    int maxApproval = ViewBag.MaxApproval;
}
<div class="fly-panel fly-panel-user" style="min-height:530px;padding-bottom:0px;padding-top:10px;">
    <div class="layui-tab layui-tab-brief" lay-filter="user">
        <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding:20px 40px 20px 0;">
            @using (Html.BeginForm(FormMethod.Post, htmlAttributes: new { @class = "layui-form", id = "myForm" }))
            {
                @Html.AntiForgeryToken()
                <div class="layui-form-item">
                    <label class="layui-form-label" for="">选择位置</label>
                    <div class="layui-input-block">
                        <select name="Step" id="Step">
                            <option value="">请选择</option>
                            @foreach (SelectListItem item in list)
                            {
                                <option value="@item.Value">@item.Text</option>
                            }
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" for="Countersign">审批人</label>
                    <div class="layui-input-block">
                        <input type="text" placeholder="请输入需要增加的审批人" class="layui-input autocomplete" value="">
                        <input type="hidden" name="empID" id="empID" value="" />
                        <input type="hidden" name="empName" id="empName" value="" />
                        <input type="hidden" name="positionID" id="positionID" value="" />
                        <input type="hidden" name="positionName" id="positionName" value="" />
                        <table class="layui-table countersignTable">
                            <thead>
                                <tr><td colspan="5" style="text-align:center;">当前流程</td></tr>
                                <tr><th style="width:60px;">步骤</th><th>审批部门</th><th>审批岗位</th><th>审批人</th><th style="width:60px;" class="center">操作</th></tr>
                            </thead>
                            <tbody>
                                @*@for (int i = 0; i < Model.CountersignEmployees.Count; i++)
                                {
                                    CountersignEmployee item = Model.CountersignEmployees[i];
                                    <tr data-id="@i" class="Countersign_@item.EmpID">
                                        <td>@item.Step</td>
                                        <td>@item.DepatrmentName</td>
                                        <td> @item.PositionName</td>
                                        <td>@item.EmpName </td>
                                        @if (item.Step < maxApproval)
                                        {
                                            <td class="center"><span class="processed">已处理</span></td>
                                        }
                                        else if (item.Step == maxApproval)
                                        {
                                            <td class="center"><span class="processing">正在处理</span></td>
                                        }
                                        else
                                        {
                                            <td class="center"><span data-step="@item.Step" data-empID="@item.EmpID" data-text="@item.DepatrmentName @item.PositionName @item.EmpName" class="delete layui-btn layui-btn-xs layui-btn-warm">删除</span></td>
                                        }
                                    </tr>
                                }*@
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="button" value="提 交" class="layui-btn mySubmit" />
                    </div>
                </div>
            }
        </div>
    </div>
</div>
<script type="text/javascript" src="/Member/Home/GetDepartmentAndPosition"></script>
<script type="text/javascript" src="~/Content/layuiadmin/department.js"></script>
<script type="text/javascript">
    layui.use(['form', 'layedit','autocomplete', 'element', 'layer'], function () {
		form = layui.form;
        layer = layui.layer;
        element = layui.element;
        autocomplete = layui.autocomplete;
        autocomplete.render({
            elem: $('.autocomplete'),
            url: '/Home/GetEmployees',
            cache: false,
            template_val: '{p:"{d.p}}",s:"{d.s}}",i:"{d.i}}",n:"{d.n}}",d:"{d.d}}",}',
            template_txt: '{{d.n}} <span class=\'layui-badge layui-bg-gray\'>{{d.d}}&nbsp;{{d.s}}&nbsp;{{d.n}}</span>',
            onselect: function (resp) {
                $(".autocomplete").val(resp.d + " " + resp.s + " " + resp.n + " ");
                $("#empID").val(resp.i);
                $("#positionID").val(resp.p);
                $("#empName").val(resp.n);
                $("#positionName").val(resp.s);
            }
        })
    });
    $(document).on("click", ".deleteItem", function () {
        $(this).parent().parent().css("display", "none");
        $(this).parent().parent().find(".Status").val("-1");
    });

    $(".mySubmit").click(function () {
        var empID = $("#empID").val();
        var positionID = $("#positionID").val();
        var empName = $("#empName").val();
        var positionName = $("#positionName").val();
        var step = $("#Step").val();
        if (step == "") {
            layer.msg("请选择节点添加位置");
            return;
        }
        var stepName = $("#Step").find("option:selected").text();;
        if ($.trim(empID).length == 0 || positionID == "" || isNaN(positionID)) {
            layer.msg("请在审批人处添加一个职员");
            return;
        }

        layer.confirm('您确定要为该流程添加如下节点：<p style="color:red;">节点:' + empName + '(' + positionName + ')</p><p style="color:red;">位置:' + stepName+'</p>', {
            btn: ['确认添加此节点', '不小心点错了'], title: "添加节点确认", area: ['400px', '200px'], //按钮
        },
        function () {
            $.ajax({
                url: $("#myForm").prop("action"),
                type: "Post",
                data: $("#myForm").serialize(),
                dataType: "json",
                beforeSend: function () {
                    layer.msg("正在提交，请稍后...");
                    $(".mySubmit").prop('disabled', true);
                },
                success: function (data) {
                    if (data.State == 200) {
                        window.location.href = window.location.href;
                    }
                    else {
                        $(".mySubmit").prop('disabled', false);
                        layer.msg(data.Msg);
                        return;
                    }
                }
            });
        },
        function () {

        }); 
    });
    $(".delete").click(function () {
        var text = $(this).attr("data-text");
        var step = $(this).attr("data-step");
        var empID = $(this).attr("data-empID");
        layer.confirm('请确认要删除该流程节点：<p style="color:red;">节点:' + text + '</p>', {
            btn: ['确认添加此节点', '不小心点错了'], title: "删除节点确认", area: ['400px', '200px'], //按钮
        },
        function () {
            var url = "/Member/CustomForm/DeletePoint?id="+@Model.ID+"&step=" + step + "&empID=" + empID;
            $.getJSON(url, function (result) {
                if (result.State == 200) {
                    window.location.href = window.location.href;
                }
                else {                    
                    layer.msg(data.Msg);
                    return;
                }
            });
        },
        function () {

        });
    });
</script>
